#!/usr/bin/python3
# _*_coding:utf8_*_

'''
 * Created with IntelliJ Pycharm.
 * Description:  
 * Project : ops-toosl
 * Ide tools : PyCharm
 * File name : test.py
 * Author <a href="mailto:3245554@qq.com">罗卫</a>
 * User: devops
 * Date: 2019/9/4
 * Time: 上午10:02
'''

# import socket
#
# # s = socket.gethostbyname('SKY-20190219IMS')
# s = socket.gethostbyname('db')
# print(s)
#
# s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# s.connect(('8.8.8.8', 80))
# ip = s.getsockname()[0]
# print(ip)

# l = "/etc/nginx/nginx.conf"
# ll = l.split('/')
# ll.remove(ll[len(ll)-1])
# ll.remove('')
# print(ll)

# import re,os
# with open('redis.conf')as f :
#     data = f.read()
#     data_list = data.split("\n")
#     os.rename('redis.conf','redis.conf.bak')
#     r = open('redis.conf','w+')
#     for  i in data_list:
#         if not re.match('^#',i) and i != '':
#             if i == 'bind 127.0.0.1':
#                 r.write('bind 0.0.0.0\n')
#         elif i == '# requirepass foobared':
#             r.write('requirepass 9aPcd5\n')
#         else:
#             r.write('%s\n'%i)
#     r.close()

# import urllib.request
#
# req =urllib.request
# req.urlretrieve('http://dl.bintray.com/gitblit/releases/gitblit-1.8.0.war','.gitee/ROOT.war')


# import pickle
#
# str = '''# mongod.conf
#
# # for documentation of all options, see:
# #   http://docs.mongodb.org/manual/reference/configuration-options/
#
# # where to write logging data.
# systemLog:
#   destination: file
#   logAppend: true
#   path: /var/log/mongodb/mongod_config.log
#
# # Where and how to store data.
# storage:
#   dbPath: /home/DB-DATA/mongo_config/
#   journal:
#     enabled: true
#   engine: wiredTiger
#   wiredTiger:
#      engineConfig:
#         cacheSizeGB: 6
#         journalCompressor: snappy
#         directoryForIndexes: false
#      collectionConfig:
#         blockCompressor: snappy
#      indexConfig:
#         prefixCompression: true
# sharding:
#     clusterRole: configsvr
# replication:
#     replSetName: configs
#
# #  engine:
# #  mmapv1:
# #  wiredTiger:
#
# # how the process runs
# processManagement:
#   fork: true  # fork and run in background
#   pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
#   timeZoneInfo: /usr/share/zoneinfo
#
# # network interfaces
# net:
#   port: 37017
#   bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#
#
# #security:
# #security:
# #  keyFile: /home/mongo-keyfile
# #  clusterAuthMode: keyFile
# #  authorization: enabled
# #operationProfiling:
#
# #replication:
#
# #sharding:
#
# ## Enterprise-Only Options
#
# #auditLog:
#
# #snmp:'''
# str1 = '''# mongod.conf
#
# # for documentation of all options, see:
# #   http://docs.mongodb.org/manual/reference/configuration-options/
#
# # where to write logging data.
# systemLog:
#   destination: file
#   logAppend: true
#   path: /var/log/mongodb/mongod_config-1.log
#
# # Where and how to store data.
# storage:
#   dbPath: /home/DB-DATA/mongo_config-1/
#   journal:
#     enabled: true
#   engine: wiredTiger
#   wiredTiger:
#      engineConfig:
#         cacheSizeGB: 6
#         journalCompressor: snappy
#         directoryForIndexes: false
#      collectionConfig:
#         blockCompressor: snappy
#      indexConfig:
#         prefixCompression: true
# sharding:
#     clusterRole: configsvr
# replication:
#     replSetName: configs
#
# #  engine:
# #  mmapv1:
# #  wiredTiger:
#
# # how the process runs
# processManagement:
#   fork: true  # fork and run in background
#   pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
#   timeZoneInfo: /usr/share/zoneinfo
#
# # network interfaces
# net:
#   port: 37018
#   bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#
#
# #security:
# #security:
# #  keyFile: /home/mongo-keyfile
# #  clusterAuthMode: keyFile
# #  authorization: enabled
# #operationProfiling:
#
# #replication:
#
# #sharding:
#
# ## Enterprise-Only Options
#
# #auditLog:
#
# #snmp:
# '''
# str2 = '''# mongod.conf
#
# # for documentation of all options, see:
# #   http://docs.mongodb.org/manual/reference/configuration-options/
#
# # where to write logging data.
# systemLog:
#   destination: file
#   logAppend: true
#   path: /var/log/mongodb/mongod_config-2.log
#
# # Where and how to store data.
# storage:
#   dbPath: /home/DB-DATA/mongo_config-2/
#   journal:
#     enabled: true
#   engine: wiredTiger
#   wiredTiger:
#      engineConfig:
#         cacheSizeGB: 6
#         journalCompressor: snappy
#         directoryForIndexes: false
#      collectionConfig:
#         blockCompressor: snappy
#      indexConfig:
#         prefixCompression: true
# sharding:
#     clusterRole: configsvr
# replication:
#     replSetName: configs
#
# #  engine:
# #  mmapv1:
# #  wiredTiger:
#
# # how the process runs
# processManagement:
#   fork: true  # fork and run in background
#   pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
#   timeZoneInfo: /usr/share/zoneinfo
#
# # network interfaces
# net:
#   port: 37019
#   bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#
#
# #security:
# #security:
# #  keyFile: /home/mongo-keyfile
# #  clusterAuthMode: keyFile
# #  authorization: enabled
# #operationProfiling:
#
# #replication:
#
# #sharding:
#
# ## Enterprise-Only Options
#
# #auditLog:
#
# #snmp:'''
# str3 = '''# mongod.conf
#
# # for documentation of all options, see:
# #   http://docs.mongodb.org/manual/reference/configuration-options/
#
# # where to write logging data.
# systemLog:
#   destination: file
#   logAppend: true
#   path: /var/log/mongodb/mongod.log
#
# # Where and how to store data.
# storage:
#   dbPath: /home/DB-DATA/mongo/
#   journal:
#     enabled: true
#   engine: wiredTiger
#   wiredTiger:
#      engineConfig:
#         cacheSizeGB: 6
#         journalCompressor: snappy
#         directoryForIndexes: false
#      collectionConfig:
#         blockCompressor: snappy
#      indexConfig:
#         prefixCompression: true
# sharding:
#     clusterRole: shardsvr
# replication:
#     replSetName: rs1
#
# #  engine:
# #  mmapv1:
# #  wiredTiger:
#
# # how the process runs
# processManagement:
#   fork: true  # fork and run in background
#   pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
#   timeZoneInfo: /usr/share/zoneinfo
#
# # network interfaces
# net:
#   port: 27017
#   bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#
#
# #security:
# #security:
# #  keyFile: /home/mongo-keyfile
# #  clusterAuthMode: keyFile
# #  authorization: enabled
# #operationProfiling:
#
# #replication:
#
# #sharding:
#
# ## Enterprise-Only Options
#
# #auditLog:
#
# #snmp:
# '''
# str4 = '''systemLog:
#     quiet: false
#     path: /var/log/mongodb/mongos.log
#     logAppend: false
#     destination: file
# sharding:
#     configDB: configs/10.0.0.67:37017,10.0.0.68:37018,10.0.0.68:37019
# processManagement:
#     fork: true
#     pidFilePath: /var/run/mongodb/mongos.pid
# net:
#     bindIp: 0.0.0.0
#     port: 31000
#     maxIncomingConnections: 10240
#     wireObjectCheck: true
#     ipv6: false
# '''
# str5 = '''123'''
# str6 = ''''''
# str7 = ''''''
# strlist = [str,str1,str2,str3,str4,str5,str6,str7]
# with open('dbconf.dat','wb')as f:
#     for i in strlist:
#         pickle.dump(i,f)
#         f.writelines('#')
# from BasicDeploymentScripts import getlanaddr
# from netaddr import *
# import psutil
# localip = getlanaddr()
#
# def up_net_car():
#     isupnetcar = psutil.net_if_stats()
#     for i in isupnetcar:
#         if isupnetcar[i].isup:
#             return i
#
# def get_ipv4_net_mask(net_car_name,confirm_ip):
#     addr = psutil.net_if_addrs()
#     net_car = addr[net_car_name]
#     print(net_car)
#     for i in net_car:
#         if i.family == 2:
#             if i.address == confirm_ip:
#                 return i.netmask
#
#def getsubnet(netmask):
    # result = ""
    # for num in netmask.split('.'):
    #     temp = str(bin(int(num)))[2:]
    #     result = result + temp
    # return len("".join(str(result).split('0')[0:1]))
#
# def byipgetCIDR(ip,subnet):
#     ips = ip.split('.')
#     if subnet <= 24:
#         ips[3] = 0
#         return ipstr(ips,subnet)
#     elif subnet <= 16:
#         ips[2] = 0
#         ips[3] = 0
#         return ipstr(ips, subnet)
#     elif subnet <= 8:
#         ips[2] = 0
#         ips[3] = 0
#         ips[1] = 0
#         return ipstr(ips, subnet)
#
# def ipstr(ips,subnet):
#     res_ip = ''
#     for i in ips:
#         res_ip += '%s.' % i
#     CIDR = '%s/%d' % (res_ip[:-1], subnet)
#     return CIDR
#
# def byCIDRiplist(CIDR):
#     ip_list = []
#     ip = IPNetwork(CIDR)
#     cidr_ip_list = list(ip)
#     for i in cidr_ip_list:
#         b = i.bin[-8::].encode('utf-8')
#         if b != b'00000000' and b != b'11111111':
#             ip_list.append(str(i))
#     return ip_list

# import socket
from BasicDeploymentScripts import *
# list_ip = ['192.168.0.1', '192.168.0.2', '192.168.0.3', '192.168.0.4', '192.168.0.5', '192.168.0.6', '192.168.0.7', '192.168.0.8', '192.168.0.9', '192.168.0.10', '192.168.0.11', '192.168.0.12', '192.168.0.13', '192.168.0.14', '192.168.0.15', '192.168.0.16', '192.168.0.17', '192.168.0.18', '192.168.0.19', '192.168.0.20', '192.168.0.21', '192.168.0.22', '192.168.0.23', '192.168.0.24', '192.168.0.25', '192.168.0.26', '192.168.0.27', '192.168.0.28', '192.168.0.29', '192.168.0.30', '192.168.0.31', '192.168.0.32', '192.168.0.33', '192.168.0.34', '192.168.0.35', '192.168.0.36', '192.168.0.37', '192.168.0.38', '192.168.0.39', '192.168.0.40', '192.168.0.41', '192.168.0.42', '192.168.0.43', '192.168.0.44', '192.168.0.45', '192.168.0.46', '192.168.0.47', '192.168.0.48', '192.168.0.49', '192.168.0.50', '192.168.0.51', '192.168.0.52', '192.168.0.53', '192.168.0.54', '192.168.0.55', '192.168.0.56', '192.168.0.57', '192.168.0.58', '192.168.0.59', '192.168.0.60', '192.168.0.61', '192.168.0.62', '192.168.0.63', '192.168.0.64', '192.168.0.65', '192.168.0.66', '192.168.0.67', '192.168.0.68', '192.168.0.69', '192.168.0.70', '192.168.0.71', '192.168.0.72', '192.168.0.73', '192.168.0.74', '192.168.0.75', '192.168.0.76', '192.168.0.77', '192.168.0.78', '192.168.0.79', '192.168.0.80', '192.168.0.81', '192.168.0.82', '192.168.0.83', '192.168.0.84', '192.168.0.85', '192.168.0.86', '192.168.0.87', '192.168.0.88', '192.168.0.89', '192.168.0.90', '192.168.0.91', '192.168.0.92', '192.168.0.93', '192.168.0.94', '192.168.0.95', '192.168.0.96', '192.168.0.97', '192.168.0.98', '192.168.0.99', '192.168.0.100', '192.168.0.101', '192.168.0.102', '192.168.0.103', '192.168.0.104', '192.168.0.105', '192.168.0.106', '192.168.0.107', '192.168.0.108', '192.168.0.109', '192.168.0.110', '192.168.0.111', '192.168.0.112', '192.168.0.113', '192.168.0.114', '192.168.0.115', '192.168.0.116', '192.168.0.117', '192.168.0.118', '192.168.0.119', '192.168.0.120', '192.168.0.121', '192.168.0.122', '192.168.0.123', '192.168.0.124', '192.168.0.125', '192.168.0.126', '192.168.0.127', '192.168.0.128', '192.168.0.129', '192.168.0.130', '192.168.0.131', '192.168.0.132', '192.168.0.133', '192.168.0.134', '192.168.0.135', '192.168.0.136', '192.168.0.137', '192.168.0.138', '192.168.0.139', '192.168.0.140', '192.168.0.141', '192.168.0.142', '192.168.0.143', '192.168.0.144', '192.168.0.145', '192.168.0.146', '192.168.0.147', '192.168.0.148', '192.168.0.149', '192.168.0.150', '192.168.0.151', '192.168.0.152', '192.168.0.153', '192.168.0.154', '192.168.0.155', '192.168.0.156', '192.168.0.157', '192.168.0.158', '192.168.0.159', '192.168.0.160', '192.168.0.161', '192.168.0.162', '192.168.0.163', '192.168.0.164', '192.168.0.165', '192.168.0.166', '192.168.0.167', '192.168.0.168', '192.168.0.169', '192.168.0.170', '192.168.0.171', '192.168.0.172', '192.168.0.173', '192.168.0.174', '192.168.0.175', '192.168.0.176', '192.168.0.177', '192.168.0.178', '192.168.0.179', '192.168.0.180', '192.168.0.181', '192.168.0.182', '192.168.0.183', '192.168.0.184', '192.168.0.185', '192.168.0.186', '192.168.0.187', '192.168.0.188', '192.168.0.189', '192.168.0.190', '192.168.0.191', '192.168.0.192', '192.168.0.193', '192.168.0.194', '192.168.0.195', '192.168.0.196', '192.168.0.197', '192.168.0.198', '192.168.0.199', '192.168.0.200', '192.168.0.201', '192.168.0.202', '192.168.0.203', '192.168.0.204', '192.168.0.205', '192.168.0.206', '192.168.0.207', '192.168.0.208', '192.168.0.209', '192.168.0.210', '192.168.0.211', '192.168.0.212', '192.168.0.213', '192.168.0.214', '192.168.0.215', '192.168.0.216', '192.168.0.217', '192.168.0.218', '192.168.0.219', '192.168.0.220', '192.168.0.221', '192.168.0.222', '192.168.0.223', '192.168.0.224', '192.168.0.225', '192.168.0.226', '192.168.0.227', '192.168.0.228', '192.168.0.229', '192.168.0.230', '192.168.0.231', '192.168.0.232', '192.168.0.233', '192.168.0.234', '192.168.0.235', '192.168.0.236', '192.168.0.237', '192.168.0.238', '192.168.0.239', '192.168.0.240', '192.168.0.241', '192.168.0.242', '192.168.0.243', '192.168.0.244', '192.168.0.245', '192.168.0.246', '192.168.0.247', '192.168.0.248', '192.168.0.249', '192.168.0.250', '192.168.0.251', '192.168.0.252', '192.168.0.253', '192.168.0.254']
# onlin_host = ping_onlin_host(list_ip)
# print(onlin_host)
# b = getlanremoteaddr('db')
#
# for i in b:
#     try:
#         host_name = socket.gethostbyaddr(i)
#         print('ip:%s -> hostname:%s'%(i,str(host_name)))
#     except Exception as e:
#         print(e)
# s = 'ping 192.168.0.140 asfasdfsa sfsfs afwqe sfsa fa fd vs f eaw f sv as f as f s fas'
# p = s.split(' ')[1]
# print(p)

# import urllib.request,shutil
#
# def moveFileto(sourceDir,  targetDir):
#     shutil.copy(sourceDir,  targetDir)
#
# download_url = 'http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.5.5/apache-zookeeper-3.5.5-bin.tar.gz'
# try:
#     urllib.request.urlretrieve(download_url, 'zookeeper.tar.gz')
# except Exception as e:
#     print(e)
# res = command('tar -zxvf zookeeper.tar.gz -C /opt/')
# zookeeper_start = '#/bin/bash\n\ncd /opt/apache-zookeeper-3.5.5-bin/bin\nchmod 775 zkServer.sh\n./zkServer.sh start'
# moveFileto('/opt/apache-zookeeper-3.5.5-bin/conf/zoo_sample.cfg','/opt/apache-zookeeper-3.5.5-bin/conf/zoo.cfg')
# writefile('tmp.sh',zookeeper_start)
# zookeeper_res = command('sh tmp.sh')
# if zookeeper_res[0] == 0:
#     print('zookeeper STARTED')
# else:
#     print('zookeeper FAILED')
# mkdir_chcek('/home/devops/mongodb/mong-r/sfas.conf')
# print(command('cat /etc/sysctl.conf |grep net.ipv4.tcp_tw_recycle'))
# res = command('openssl rand -base64 512')
# writefile('auth_key',res[1])
# print(res)
